package com.sinoglobal.ningxia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.EnentCodeVo;
import com.sinoglobal.ningxia.beans.EnentItemVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;

import net.tsz.afinal.FinalBitmap;

/**
 * 
 * @author zbl
 * @createdate 2014-7-9 上午9:49:06
 * @Description:首页活动的详情页
 */
public class EnentItemActivity extends BaseActivity implements IBase {
	private ImageView enentsImg;// 活动的img
	private TextView enentsName;// 活动名称
	private TextView enentsDate;// 活动的有效期
	private TextView enentsContent;// 活动的详细介绍
	private Button enentsCode;// 购买
	private String id;// 活动的ID

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isTemplate = true;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enent_item);
		titleView.setText(R.string.events_item);

		openChenJInShi();//开启沉浸式
		Intent intent = getIntent();
		id = intent.getStringExtra("id");
		init();// 初始化控件
		loadInfo();// 从网络获取数据
		showListener();
		// 获取传递过来的活动ID
	}

	@Override
	public void init() {
		enentsImg = (ImageView) findViewById(R.id.shopping_img);
		enentsName = (TextView) findViewById(R.id.mallshop_goodsName);
		enentsDate = (TextView) findViewById(R.id.mallshop_goods_date);
		enentsContent = (TextView) findViewById(R.id.goods_content);
		enentsCode = (Button) findViewById(R.id.goods_code);

	}

	@Override
	public void showListener() {
		// 领取活动的编码
		enentsCode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 判断用户是否登录

				if (isLoginIntentLoginActivity()) {
					ItktLog.i("------------是否登录？---------**" + isLoginIntentLoginActivity());
					// enents_code.setClickable(true);
					new ItktAsyncTaskWithDialog<Void, Void, EnentCodeVo>(getString(R.string.loading), true, true) {

						@Override
						public EnentCodeVo before(Void... params) throws Exception {
							enentsCode.setClickable(false);
							ItktLog.i("---------**用户ID？**--------**" + FlyApplication.userId);
							return RemoteImpl.getInstance().getEnentCode(FlyApplication.userId, id);
						}

						@Override
						public void after(EnentCodeVo result) {
							if (result != null) {
								if (Code.CODE_SUCCESS != result.getCode()) {
									enentsCode.setClickable(true);
									showShortToastMessage(result.getMessage());
								} else {
									enentsCode.setText(result.getAct_code());
									enentsCode.setBackgroundResource(R.drawable.bg_enentitem_btn);
									enentsCode.setClickable(false);
								}
							}
						}

						@Override
						public void exception() {

						}

					}.execute();

				}
			}
		});

	}

	// 访问网络获取活动的详情信息
	private void loadInfo() {
		new ItktAsyncTaskWithDialog<Void, Void, EnentItemVo>(getString(R.string.loading), true, true) {

			@Override
			public EnentItemVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getEnentItem(id);
			}

			@Override
			public void after(EnentItemVo result) {
				if (result != null) {
					if (Code.CODE_SUCCESS != result.getCode()) {
						showShortToastMessage(result.getMessage());
					} else {
						// 将获取的数据添加到布局文件
						enentsCode.setClickable(true);
						FinalBitmap fb = FinalBitmap.create(EnentItemActivity.this);
						fb.configLoadfailImage(R.drawable.bg_activities_img);
						fb.configLoadingImage(R.drawable.bg_activities_img);
						String URL = ConnectionUtil.localUrl + result.getAct_img();
						// 设置图片的宽高
						int width = FlyApplication.widthPixels;
						int height = width / 2;
						enentsImg.setLayoutParams(new LinearLayout.LayoutParams(width, height));

						fb.display(enentsImg, URL, width, height);
						enentsName.setText(result.getAct_name());
						// 对有效日期进行处理
						// int strat_index = result.getAct_start().indexOf(" ");
						// String start = result.getAct_start().substring(0,
						// strat_index);
						// int end_index = result.getAct_end().indexOf(" ");
						// String end = result.getAct_end().substring(0,
						// end_index);

						enentsDate.requestFocus();// 需要焦点
						enentsContent.setText(result.getAct_content());
						if (!MContants.EMPTY_STRING.equals(result.getAct_code())) {
							enentsCode.setText(result.getAct_code());
							enentsCode.setBackgroundResource(R.drawable.bg_enentitem_btn);
							enentsCode.setClickable(false);
						}
						enentsDate.setText(result.getAct_start() + "至" + result.getAct_end());
						enentsDate.setFocusable(true);
						enentsDate.setFocusableInTouchMode(true);
					}
				}
			}

			@Override
			public void exception() {

			}

		}.execute();
	}

}
